/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dethi;

import java.util.Scanner;

/**
 *
 * @author Administrator
 */
public class QuanlySanPham {

    private static final int SIZE = 10;
    private SanPham[] dsSanPham;

    public QuanlySanPham() {
        this.dsSanPham = new SanPham[SIZE];
    }

    /*
     * Nhap thong tin san pham 
     */
    public void nhapSanPham() {
        Scanner s = new Scanner(System.in);
        System.out.println("Nhap danh sach san pham:");

        for (int i = 0; i < SIZE; i++) {
            System.out.println("Nhap san pham thu: " + (i + 1));
            System.out.println("Nhap ma san pham:");
            int masp = s.nextInt();
            if (masp == 1) {
                break;
            }
            System.out.println("Nhap ten san pham:");
            String tensp = s.next();
            System.out.println("Nhap gia san pham");
            int giasp = s.nextInt();
            SanPham sp = new SanPham(masp, tensp, giasp);
            this.dsSanPham[i] = sp;

        }
    }
    /*
     * Hien thi 
     */

    public void hienthiSP() {
        System.out.println("Danh sach san pham");
        System.out.println("MaSP\t TenSP\t Gia");
        for (int i = 0; i < SIZE; i++) {
            if (this.dsSanPham[i] != null) {
                int masp = this.dsSanPham[i].getMasp();
                String tensp = this.dsSanPham[i].getTensp();
                int giaSP = this.dsSanPham[i].getGia();
                System.out.println(masp + "\t" + tensp + "\t" + giaSP);
            }
        }
        SanPham spGiaCao = this.spGiaCaoNhat();
        SanPham spGiaThap = this.spGiaThapNhat();
        double giaTB = this.giaTB();

        System.out.println("San Pham co gia cao nhat: " + spGiaCao.getTensp() + " " + spGiaCao.getGia());
        System.out.println("San Pham co gia thap nhat: " + spGiaThap.getTensp() + " " + spGiaThap.getGia());
        System.out.println("Gia trung binh cua cac san pham: " + giaTB);
    }
    /*
     * San pham co gia cao nhat
     */

    public SanPham spGiaCaoNhat() {
        SanPham s = this.dsSanPham[0];
        for (int i = 1; i < SIZE; i++) {
            if (this.dsSanPham[i] != null) {
                if (s.getGia() < this.dsSanPham[i].getGia()) {
                    s = this.dsSanPham[i];
                }
            }
        }
        return s;
    }

    /*
     * San pham co gia thap nhat
     */
    public SanPham spGiaThapNhat() {
        SanPham s = this.dsSanPham[0];
        for (int i = 1; i < SIZE; i++) {
            if (this.dsSanPham[i] != null) {
                if (s.getGia() > this.dsSanPham[i].getGia()) {
                    s = this.dsSanPham[i];
                }
            }
        }
        return s;
    }
    /*
     * tinh trung binh gia
     */

    public double giaTB() {
        double sum = 0.0;
        int count = 0;
        for (int i = 0; i < SIZE; i++) {
            if (this.dsSanPham[i] != null) {
                count++;
                sum += this.dsSanPham[i].getGia();
            }
        }

        return sum / count;

    }
    /*
     * Hien thi san pham theo gia : gia +- 10%
     * 
     */

    public void hienthiSPTheoGia() {
        System.out.println("Nhap vao gia cua san pham:");
        Scanner s = new Scanner(System.in);
        int giaChuan = s.nextInt();
        double giaNho = giaChuan * 0.9;
        double giaLon = giaChuan * 1.1;
        System.out.println("MaSP\t TenSP\t Gia");
        for (int i = 0; i < SIZE; i++) {
            if (this.dsSanPham[i] != null) {
                if (this.dsSanPham[i].getGia() >= giaNho && this.dsSanPham[i].getGia() <= giaLon) {
                   int masp = this.dsSanPham[i].getMasp();
                   String tensp = this.dsSanPham[i].getTensp();
                   int giaSP = this.dsSanPham[i].getGia();
                   System.out.println(masp + "\t" + tensp + "\t" + giaSP);
                }
            }
        }

    }

    /*
     * Unit test
     */
    public static void main(String[] args) {
        QuanlySanPham s = new QuanlySanPham();
        s.nhapSanPham();
        s.hienthiSP();
        s.hienthiSPTheoGia();


    }
}
